package com.shanghaiyimu.yimiyangche.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.activity.GoodsContentActivity;
import com.shanghaiyimu.yimiyangche.activity.GoodsListActivity;
import com.shanghaiyimu.yimiyangche.bean.GoodsBean;

import java.util.List;

/**
 * Created by Administrator on 2017/7/4.
 */

public class PullToRefreshAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsBean> list;

    public PullToRefreshAdapter(Context context,List<GoodsBean> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView , ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.goods,viewGroup,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(list.get(position).getName());
        holder.content.setText(list.get(position).getContent());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                if (position%2 == 0) {
                    intent.setClass(context, GoodsContentActivity.class);
                    intent.putExtra("name", position + "");
                    context.startActivity(intent);
                }else {
                    intent.setClass(context,GoodsListActivity.class);
                    intent.putExtra("name", position + "");
                    context.startActivity(intent);
                }
            }
        });
        return convertView;
    }
    class ViewHolder{

        TextView name,content ;

        public ViewHolder(View view){

            name = (TextView) view.findViewById(R.id.goods_textView_name);
            content = (TextView) view.findViewById(R.id.goods_text_content);

        }


    }
}
